package com.flick.shopSales.service.impl;

import com.flick.baseService.impl.BaseServImpl;
import com.flick.po.ShopSales;
import com.flick.shopSales.dao.ShopSalesDao;
import com.flick.shopSales.service.ShopSalesServ;
import com.flick.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by flick on 2017/4/23.
 */
@Service
public class ShopSalesServImpl extends BaseServImpl<ShopSales> implements ShopSalesServ {

    public ResponseEntity<byte[]> downloadExcel() throws IOException {
        List<ShopSales> list = list();
        String[] title = new String[7];
        title[0] = "商品名称";
        title[1] = "分类名称";
        title[2] = "价格";
        title[3] = "市场价";
        title[4] = "品牌";
        title[5] = "库存";
        title[6] = "销售数量";
        List<Object[]> excelList = new ArrayList<Object[]>();
        for (ShopSales shopSales:list) {
            Object[] objs = new Object[7];
            objs[0] = shopSales.getShopInfo().getTitle();
            objs[1] = shopSales.getShopInfo().getShopType().getTypeName();
            objs[2] = shopSales.getShopInfo().getPrice();
            objs[3] = shopSales.getShopInfo().getMarkprice();
            objs[4] = shopSales.getShopInfo().getBrand();
            objs[5] = shopSales.getProductStock();
            objs[6] = shopSales.getSalesNum();
            excelList.add(objs);
        }
        InputStream is = ExcelUtil.getInputStream("商品销售情况表",title,excelList);
        byte[] body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=shopSales.xls");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
}
